package com.zhao.modleclass.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;
import com.zhao.modleclass.Helper.BitmapCircleTransformation;
import com.zhao.modleclass.R;
import com.zhao.modleclass.activity.HPTagListActivity;
import com.zhao.modleclass.activity.SC_Teacher;
import com.zhao.modleclass.activity.SC_TeacherWebView;
import com.zhao.modleclass.adapter.BiaoQianAdapter;
import com.zhao.modleclass.adapter.SC_MoDenTeacherAdapter;
import com.zhao.modleclass.adapter.SC_TeacherRActivityAdapter;
import com.zhao.modleclass.bean.SearchClassBiaoQianBean;
import com.zhao.modleclass.presenter.SearchPresenter;
import com.zhao.modleclass.utils.Constant;
import com.zhao.modleclass.utils.SpacesItemDecoration;
import com.zhao.modleclass.view.ISearchCLassView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by 张昭 on 2016/11/29.
 */

public class SearchClassFragmentText extends Fragment implements ISearchCLassView {


    @Bind(R.id.image_fragment_search_nameIcon1)
    ImageView imageFragmentSearchNameIcon1;
    @Bind(R.id.textView_fragment_search_name1)
    TextView textViewFragmentSearchName1;
    @Bind(R.id.image_fragment_search_nameIcon2)
    ImageView imageFragmentSearchNameIcon2;
    @Bind(R.id.textView_fragment_search_name2)
    TextView textViewFragmentSearchName2;
    @Bind(R.id.image_fragment_search_nameIcon3)
    ImageView imageFragmentSearchNameIcon3;
    @Bind(R.id.textView_fragment_search_name3)
    TextView textViewFragmentSearchName3;
    @Bind(R.id.image_fragment_search_nameIcon4)
    ImageView imageFragmentSearchNameIcon4;
    @Bind(R.id.textView_fragment_search_name4)
    TextView textViewFragmentSearchName4;
    @Bind(R.id.image_fragment_search_nameIcon5)
    ImageView imageFragmentSearchNameIcon5;
    @Bind(R.id.textView_fragment_search_name5)
    TextView textViewFragmentSearchName5;
    @Bind(R.id.image_fragment_search_nameIcon6)
    ImageView imageFragmentSearchNameIcon6;
    @Bind(R.id.textView_fragment_search_name6)
    TextView textViewFragmentSearchName6;
    @Bind(R.id.image_fragment_search_nameIcon7)
    ImageView imageFragmentSearchNameIcon7;
    @Bind(R.id.textView_fragment_search_name7)
    TextView textViewFragmentSearchName7;
    @Bind(R.id.image_fragment_search_nameImag1)
    ImageView imageFragmentSearchNameImag1;
    @Bind(R.id.textView_fragment_search_tatle1)
    TextView textViewFragmentSearchTatle1;
    @Bind(R.id.image_fragment_search_nameImag2)
    ImageView imageFragmentSearchNameImag2;
    @Bind(R.id.textView_fragment_search_tatle2)
    TextView textViewFragmentSearchTatle2;
    @Bind(R.id.image_fragment_search_nameImag3)
    ImageView imageFragmentSearchNameImag3;
    @Bind(R.id.textView_fragment_search_tatle3)
    TextView textViewFragmentSearchTatle3;
    @Bind(R.id.image_fragment_search_nameImag4)
    ImageView imageFragmentSearchNameImag4;
    @Bind(R.id.textView_fragment_search_tatle4)
    TextView textViewFragmentSearchTatle4;
    @Bind(R.id.image_fragment_search_nameImag5)
    ImageView imageFragmentSearchNameImag5;
    @Bind(R.id.textView_fragment_search_tatle5)
    TextView textViewFragmentSearchTatle5;
    @Bind(R.id.llSV_fragment_searcherclass_a)
    LinearLayout llSVFragmentSearcherclassA;
    @Bind(R.id.llSV_fragment_searcherclass_b)
    LinearLayout llSVFragmentSearcherclassB;
    @Bind(R.id.llSV_fragment_searcherclass_c)
    LinearLayout llSVFragmentSearcherclassC;
    @Bind(R.id.llSV_fragment_searcherclass_d)
    LinearLayout llSVFragmentSearcherclassD;
    @Bind(R.id.llSV_fragment_searcherclass_e)
    LinearLayout llSVFragmentSearcherclassE;
    @Bind(R.id.llSV_fragment_searcherclass_f)
    LinearLayout llSVFragmentSearcherclassF;
    @Bind(R.id.llSV_fragment_searcherclass_g)
    LinearLayout llSVFragmentSearcherclassG;
    @Bind(R.id.textView)
    TextView textView;
    @Bind(R.id.fragment_searchclass_rclv)
    RecyclerView fragmentSearchclassRclv;
    @Bind(R.id.fragment_searchclass_PullToRefresh)
    PullToRefreshScrollView fragmentSearchclassPullToRefresh;
    @Bind(R.id.linnearLayout_TagA)
    LinearLayout linnearLayoutTagA;
    @Bind(R.id.linnearLayout_TagB)
    LinearLayout linnearLayoutTagB;
    @Bind(R.id.linnearLayout_TagD)
    LinearLayout linnearLayoutTagD;
    @Bind(R.id.linnearLayout_TagF)
    LinearLayout linnearLayoutTagF;
    @Bind(R.id.linnearLayout_TagGF)
    LinearLayout linnearLayoutTagG;
    private RecyclerView recyclerView_fragmnet;

    private TextView textView_fragment;
    private Context mContext;
    private SearchPresenter searchPresenter;
    private PullToRefreshScrollView scrollView;
    private int page=2;
    int tage=0;
    private LinearLayout linearLayout_all;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getActivity();

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_searchclass, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        //更多标签
        recyclerView_fragmnet = (RecyclerView) view.findViewById(R.id.fragment_searchclass_rclv);
        scrollView = (PullToRefreshScrollView) view.findViewById(R.id.fragment_searchclass_PullToRefresh);
        linearLayout_all = (LinearLayout) view.findViewById(R.id.LinearLayout_fragment_searchclass_all);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initDataMore();
        linearLayout_all.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, SC_Teacher.class);
                startActivity(intent);
            }
        });
    }


    private void initDataMore() {
        //更多标签
        GridLayoutManager mGManager = new GridLayoutManager(mContext, 2);
        recyclerView_fragmnet.addItemDecoration(new SpacesItemDecoration(3));
        recyclerView_fragmnet.setLayoutManager(mGManager);
        searchPresenter = new SearchPresenter(this);
        final Map<String, String> map = new HashMap<>();
        map.put("pageNo", "1");
        map.put("pageSize", "6");
        searchPresenter.login(mContext, "findList.do", map,recyclerView_fragmnet,tage);
        scrollView.setMode(PullToRefreshBase.Mode.BOTH);
        scrollView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ScrollView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ScrollView> refreshView) {
                tage=0;
                map.put("pageNo", "1");
                map.put("pageSize", "6");
                searchPresenter.login(mContext, "findList.do", map,recyclerView_fragmnet,tage);
                scrollView.onRefreshComplete();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ScrollView> refreshView) {
                tage=1;
                map.put("pageNo", page+"");
                map.put("pageSize", "6");
                searchPresenter.login(mContext, "findList.do", map,recyclerView_fragmnet,tage);
                page++;
                scrollView.onRefreshComplete();
            }
        });

    }


    @Override
    public void toSetAdapter(BiaoQianAdapter adapter) {
        if (adapter != null) {
            recyclerView_fragmnet.setAdapter(adapter);
        }
    }

    @Override
    public void showError(Exception e) {
        Toast.makeText(mContext, e.getMessage(), Toast.LENGTH_LONG).show();
    }

    @Override
    public void getTeacher(final SearchClassBiaoQianBean searchClassBiaoQianBean) {
        Glide.with(mContext).
                load(searchClassBiaoQianBean.getHotTeacther().get(0).getTechHeadUrl())
                .transform(new BitmapCircleTransformation(mContext))
                .into(imageFragmentSearchNameIcon1);
        textViewFragmentSearchName1.setText(searchClassBiaoQianBean.getHotTeacther().get(0).getTechNickName());

        Glide.with(mContext).
                load(searchClassBiaoQianBean.getHotTeacther().get(1).getTechHeadUrl())
                .transform(new BitmapCircleTransformation(mContext))
                .into(imageFragmentSearchNameIcon2);
        textViewFragmentSearchName2.setText(searchClassBiaoQianBean.getHotTeacther().get(1).getTechNickName());

        Glide.with(mContext).
                load(searchClassBiaoQianBean.getHotTeacther().get(2).getTechHeadUrl())
                .transform(new BitmapCircleTransformation(mContext))
                .into(imageFragmentSearchNameIcon3);
        textViewFragmentSearchName3.setText(searchClassBiaoQianBean.getHotTeacther().get(2).getTechNickName());

        Glide.with(mContext).
                load(searchClassBiaoQianBean.getHotTeacther().get(3).getTechHeadUrl())
                .transform(new BitmapCircleTransformation(mContext))
                .into(imageFragmentSearchNameIcon4);
        textViewFragmentSearchName4.setText(searchClassBiaoQianBean.getHotTeacther().get(3).getTechNickName());

        Glide.with(mContext).
                load(searchClassBiaoQianBean.getHotTeacther().get(4).getTechHeadUrl())
                .transform(new BitmapCircleTransformation(mContext))
                .into(imageFragmentSearchNameIcon5);
        textViewFragmentSearchName5.setText(searchClassBiaoQianBean.getHotTeacther().get(4).getTechNickName());

        Glide.with(mContext).
                load(searchClassBiaoQianBean.getHotTeacther().get(5).getTechHeadUrl())
                .transform(new BitmapCircleTransformation(mContext))
                .into(imageFragmentSearchNameIcon6);
        textViewFragmentSearchName6.setText(searchClassBiaoQianBean.getHotTeacther().get(5).getTechNickName());

        Glide.with(mContext).
                load(searchClassBiaoQianBean.getHotTeacther().get(6).getTechHeadUrl())
                .transform(new BitmapCircleTransformation(mContext))
                .into(imageFragmentSearchNameIcon7);
        textViewFragmentSearchName7.setText(searchClassBiaoQianBean.getHotTeacther().get(6).getTechNickName());
        List<LinearLayout> mList_ll = new ArrayList<>();
        mList_ll.add(llSVFragmentSearcherclassA);
        mList_ll.add(llSVFragmentSearcherclassB);
        mList_ll.add(llSVFragmentSearcherclassC);
        mList_ll.add(llSVFragmentSearcherclassD);
        mList_ll.add(llSVFragmentSearcherclassE);
        mList_ll.add(llSVFragmentSearcherclassF);
        mList_ll.add(llSVFragmentSearcherclassG);
        for (int j = 0; j < mList_ll.size(); j++) {
            final int cont = j;
            mList_ll.get(j).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String url = Constant.Item_URL + searchClassBiaoQianBean.getHotTeacther().get(cont).getTechId();
                    String titleName = searchClassBiaoQianBean.getHotTeacther().get(cont).getTechNickName();
                    Bundle bundle = new Bundle();
                    bundle.putString("url", url);
                    bundle.putString("name", titleName);
                    //intent跳转
                    Intent intent = new Intent(mContext, SC_TeacherWebView.class);
                    intent.putExtra("url", bundle);
                    intent.putExtra("name", bundle);
                    //启动跳转
                    mContext.startActivity(intent);
                }
            });
        }


    }

    @Override
    public void getTatle(final List<SearchClassBiaoQianBean.TagListBean> mList) {
        Glide.with(mContext).
                load(mList.get(0).getTagImageUrl())
                .transform(new BitmapCircleTransformation(mContext))
                .into(imageFragmentSearchNameImag1);
        textViewFragmentSearchTatle1.setText(mList.get(0).getTagName());

        Glide.with(mContext).
                load(mList.get(1).getTagImageUrl())
                .transform(new BitmapCircleTransformation(mContext))
                .into(imageFragmentSearchNameImag2);
        textViewFragmentSearchTatle2.setText(mList.get(1).getTagName());

        Glide.with(mContext).
                load(mList.get(2).getTagImageUrl())
                .transform(new BitmapCircleTransformation(mContext))
                .into(imageFragmentSearchNameImag3);
        textViewFragmentSearchTatle3.setText(mList.get(2).getTagName());

        Glide.with(mContext).
                load(mList.get(3).getTagImageUrl())
                .transform(new BitmapCircleTransformation(mContext))
                .into(imageFragmentSearchNameImag4);
        textViewFragmentSearchTatle4.setText(mList.get(3).getTagName());

        Glide.with(mContext).
                load(mList.get(4).getTagImageUrl())
                .transform(new BitmapCircleTransformation(mContext))
                .into(imageFragmentSearchNameImag5);
        textViewFragmentSearchTatle5.setText(mList.get(4).getTagName());
        List<LinearLayout> mList_llA = new ArrayList<>();
        mList_llA.add(linnearLayoutTagA);
        mList_llA.add(linnearLayoutTagB);
        mList_llA.add(linnearLayoutTagD);
        mList_llA.add(linnearLayoutTagF);
        mList_llA.add(linnearLayoutTagG);
        for (int j = 0; j < mList_llA.size(); j++) {
            final int con = j;
            mList_llA.get(j).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(getActivity(), HPTagListActivity.class);
                    intent.putExtra("tagId", mList.get(con).getTagId());
                    intent.putExtra("tagName",mList.get(con).getTagName());
                    startActivity(intent);
                }
            });
        }
    }

    @Override
    public void toSetItemAdapter(SC_MoDenTeacherAdapter tAdapter) {

    }

    @Override
    public void toSetRAdapter(SC_TeacherRActivityAdapter adapter) {

    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
